/*
 * Copyright © 2017 eqxiu.com 北京中网易企秀科技有限公司  All rights reserved.
 */
package cn.hermit.data.json.model;

import cn.hermit.util.ReflectionUtils;
import cn.hermit.util.StringUtils;

import java.util.Collection;
import java.util.Iterator;

/**
 * Java Model of JSON Array
 * 
 * @author Jack Gao (Chinese name : GAO JIANGUO, Email : linux.gjg@gmail.com)
 * @date 13 Jun, 2014
 */
public class JSONArray extends JSONDocument {

    /**
	 * 
	 */
    private static final long serialVersionUID = -7998487715137863812L;

    Collection<JSONDocument> values;

    JSONArray() {

    }

    /**
     * Clear JSON objects from JSON Array
     */
    public void clear() {
        values.clear();
    }

    /**
     * @return
     * 		Iterator for JSON objects
     */
    public Iterator<JSONDocument> valueIterator() {
        return values.iterator();
    }

    /**
     * @return
     * 		JSON Array's size
     */
    public int size() {
        return values.size();
    }

    /**
     * Serialize JSON Array to JSON string without tab indents
     */
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append('[');
        if (!ReflectionUtils.isEmpty(values)) {
            Iterator<JSONDocument> iter = values.iterator();
            while (iter.hasNext()) {
                JSONDocument value = iter.next();
                if (JSONValue.class.isInstance(value)) {
                    JSONValue jsv = (JSONValue) value;
                    if (jsv.isText()) {
                        sb.append('"');
                    }
                    sb.append(value);
                    if (jsv.isText()) {
                        sb.append('"');
                    }
                } else {
                    sb.append(value);
                }
                if (iter.hasNext())
                    sb.append(',');
            }
        }
        sb.append(']');
        return sb.toString();
    }

    @Override
    String toFormatString(int depth, String newLineFeed, boolean isTop) {
        StringBuilder sb = new StringBuilder();
        if (isTop) {
            sb.append(StringUtils.tabs(depth));
        }
        sb.append('[');
        if (!ReflectionUtils.isEmpty(values)) {
            sb.append(newLineFeed);
            Iterator<JSONDocument> iter = values.iterator();
            while (iter.hasNext()) {
                JSONDocument value = iter.next();
                if (JSONValue.class.isInstance(value)) {
                    JSONValue jsv = (JSONValue) value;
                    if (jsv.isText()) {
                        sb.append(StringUtils.tabs(depth + 1) + '"');
                    }
                    sb.append(value.toString());
                    if (jsv.isText()) {
                        sb.append('"');
                    }
                } else {
                    sb.append(value.toFormatString(depth + 1, newLineFeed));
                }
                if (iter.hasNext())
                    sb.append(',').append(newLineFeed);
            }
            sb.append(newLineFeed).append(StringUtils.tabs(depth));
        }
        sb.append(']');
        return sb.toString();
    }

    @Override
    public boolean isArray() {
        return true;
    }

    @Override
    public boolean isObject() {
        return false;
    }

    @Override
    public boolean isValue() {
        return false;
    }

    @Override
    public boolean isTextValue() {
        return false;
    }

}